package com.jhgsys.internal.sypq.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.jhgsys.internal.common.utils.DateUtil;
import com.jhgsys.internal.sypq.entity.Calendar;
import com.jhgsys.internal.sypq.entity.Notice;
import com.jhgsys.internal.sypq.mapper.CalendarMapper;
import com.jhgsys.internal.sypq.service.ICalendarService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.sypq.service.INoticeService;
import com.jhgsys.internal.system.entity.User;
import org.apache.shiro.SecurityUtils;
import com.jhgsys.internal.common.entity.FebsConstant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 日历管理 Service实现
 *
 * @author ganjun
 * @date 2024-11-21 16:56:51
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CalendarServiceImpl extends ServiceImpl<CalendarMapper, Calendar> implements ICalendarService {

    @Resource
    INoticeService noticeService;

    /**
     * 查询（分页）
     * @author ganjun
     * @date 2024-11-21 16:56:51
     * @param request QueryRequest
     * @param calendar calendar
     * @return IPage<Calendar>
    */
    @Override
    public IPage<Calendar> findCalendarPage(QueryRequest request, Calendar calendar) {
        LambdaQueryWrapper<Calendar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Calendar::getDeletedFlg, 0);

        Page<Calendar> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author ganjun
     * @date 2024-11-21 16:56:51
     * @param calendar calendar
     * @return List<Calendar>
     */
    @Override
    public List<Calendar> findCalendarList(Calendar calendar) {
	    LambdaQueryWrapper<Calendar> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Calendar::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author ganjun
    * @date 2024-11-21 16:56:51
    * @param id
    * @return Calendar
    */
    @Override
    public Calendar findCalendarById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author ganjun
     * @date 2024-11-21 16:56:51
     * @param calendar calendar
     */
    @Override
    @Transactional
    public boolean createCalendar(Calendar calendar) {
        boolean res;
        res = this.save(calendar);
        //添加通知
        String content = DateUtil.getDateFormat(calendar.getStartTime(), "yyyy-MM-dd HH:mm") + "到" + DateUtil.getDateFormat(calendar.getEndTime(), "yyyy-MM-dd HH:mm") + "计划锁定，锁定备注（" + calendar.getMemo() + "），该期间所有资源无法使用，请及时关注";
        Notice notice = new Notice();
        notice.setNoticeType(1);
        notice.setPendingType("日历通知");
        notice.setNoticeTitle("日历锁定");
        notice.setNoticeContent(content);
        notice.setReceiveUserId("");
        notice.setReceiveRealName("");
        notice.setSendDate(new Date());
        noticeService.createNotice(notice);
        return res;
    }

    /**
     * 修改
     * @author ganjun
     * @date 2024-11-21 16:56:51
     * @param calendar calendar
     */
    @Override
    @Transactional
    public boolean updateCalendar(Calendar calendar) {
        return this.updateById(calendar);
    }

    /**
     * 逻辑删除
     * @author ganjun
     * @date 2024-11-21 16:56:51
     * @param calendar calendar
     * @param calendarIds calendarIds
     */
    @Override
    @Transactional
    public boolean deleteCalendar(Calendar calendar,String calendarIds) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> list = Arrays.asList(calendarIds.split(StringPool.COMMA));

        calendar.setDeletedBy(user.getUserName());
        calendar.setDeletedTime(new Date());
        calendar.setDeletedFlg(FebsConstant.INVALID);
        int update = this.baseMapper.update(calendar,new LambdaQueryWrapper<Calendar>().in(Calendar::getId,list));
        if (update > 0 ){
            result = true;
        }
        if(result){
            //添加通知
            LambdaQueryWrapper<Calendar> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Calendar::getId,list);
            queryWrapper.eq(Calendar::getDeletedFlg, "1");
            List<Calendar> calendarList = this.baseMapper.selectList(queryWrapper);
            if(CollectionUtil.isNotEmpty(calendarList)){
                for(Calendar c : calendarList){
                    String content = DateUtil.getDateFormat(c.getStartTime(), "yyyy-MM-dd HH:mm")   + "到" + DateUtil.getDateFormat(c.getEndTime(), "yyyy-MM-dd HH:mm") + "锁定取消，该期间所有资源正常使用，请及时关注";
                    Notice notice = new Notice();
                    notice.setNoticeType(1);
                    notice.setPendingType("日历通知");
                    notice.setNoticeTitle("日历锁定解除");
                    notice.setNoticeContent(content);
                    notice.setReceiveUserId("");
                    notice.setReceiveRealName("");
                    notice.setSendDate(new Date());
                    noticeService.createNotice(notice);
                }

            }

        }
        return result;
	}

    /**
    * 物理删除
    * @author ganjun
    * @date 2024-11-21 16:56:51
    * @param calendarIds calendarIds
    */
    @Override
    @Transactional
    public boolean physicallyDeleteCalendar(String calendarIds) {
        List<String> list = Arrays.asList(calendarIds.split(StringPool.COMMA));
        LambdaQueryWrapper<Calendar> wapper = new LambdaQueryWrapper<>();
        wapper.in(Calendar::getId,list);
        return this.remove(wapper);
    }
}
